﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = (int)numericUpDown1.Value;
            int leng = (int)numericUpDown2.Value;
            per1 = (double)numericUpDown3.Value;
            per2 = (double)numericUpDown4.Value;
            th1 = ((int)numericUpDown5.Value) * Math.PI / 180;
            th2 = ((int)numericUpDown6.Value) * Math.PI / 180;

            if (graphics == null) graphics = this.CreateGraphics(); 
            drawCayleyTree(n, 400, 400, leng, -Math.PI / 2);
        }

        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;

        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            if(radioButton1.Checked)
                graphics.DrawLine(
                Pens.Blue,
                (int)x0, (int)y0, (int)x1, (int)y1);
            else if(radioButton2.Checked)
                graphics.DrawLine(
                Pens.Red,
                (int)x0, (int)y0, (int)x1, (int)y1);
            else if(radioButton3.Checked)
                graphics.DrawLine(
                Pens.Green,
                (int)x0, (int)y0, (int)x1, (int)y1);
            else if(radioButton4.Checked)
                graphics.DrawLine(
                Pens.Black,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }
    }
}
